package com.ryujung.hash;

import java.util.HashMap;

public class MyHashMapWithoutGeneric {

    private Node[] arr;
    transient int size = 0;
    double factor = 0.75d;
    public static final int DEFAULT_SIZE = 1 << 4;
    public static final int MAX_SIZE = 1 << 30;

    public MyHashMapWithoutGeneric() {
    }

    public MyHashMapWithoutGeneric(int size) {
        // 重点在于获取大于size的最小2进制值
        int initSize = size >= MAX_SIZE ? MAX_SIZE : tableSizeFor(size);
        arr = new Node[initSize];
    }

    /**
     * JDK11和JDK8中算法不同
     */
    private static int tableSizeFor(int size) {
        int n = -1 >>> Integer.numberOfLeadingZeros(size - 1);
        return (n < 0) ? 1 : (n >= MAX_SIZE) ? MAX_SIZE : n + 1;
    }

    public Integer put(int k, int v) {
        int hash = hash(k);
        if (contains(k)) {
            int oldVal = get(k);
            return oldVal;
        }
        if (this.arr == null) {
            this.arr = new Node[DEFAULT_SIZE];
        } else if (this.size + 1 >= arr.length * factor) {
            resize();
        }

        Node newNode = new Node(hash, k, v, null);
        int binNum = hash & (arr.length - 1);
        if (arr[binNum] != null) {
            Node node = arr[binNum];
            while (node.next != null) {
                node = node.next;
            }
            node.next = newNode;
        } else {
            this.size++;
            arr[binNum] = newNode;
        }
        return null;
    }

    public Integer get(int k) {
        int hash = hash(k);
        int binNum = hash & (arr.length - 1);
        Node curNode = arr[binNum];
        while (curNode != null) {
            if (curNode.key != k) {
                curNode = curNode.next;
            } else {
                return curNode.value;
            }
        }
        return null;
    }

    private int hash(int val) {
        return val * 3 + 2;
    }

    public void resize() {
        int newCap = arr.length << 1;
        Node[] oldArr = this.arr;
        Node[] newTable = new Node[newCap];
        int newSize = 0;
        for (int i = 0; i < oldArr.length; i++) {
            Node node = arr[i];
            while (node != null) {
                if (newTable[node.hash & (newCap - 1)] == null) {
                    newTable[node.hash & (newCap - 1)] = node;
                    newSize++;
                } else {
                    Node tail = newTable[node.hash & (newCap - 1)];
                    while (tail.next != null) {
                        tail = tail.next;
                    }
                    tail.next = node;
                }
                node = node.next;
            }
        }
        this.size = newSize;
        this.arr = (Node[]) newTable;
    }

    public void remove(int value) {
        if (!contains(value)) {
            return;
        }

        int hash = hash(value);
        int binNum = hash & (arr.length - 1);
        if (arr[binNum] != null) {
            Node curNode = arr[binNum];
            if (curNode.value == value) {
                arr[binNum] = curNode.next;
                if (arr[binNum] == null) {
                    this.size--;
                }
                return;
            }
            Node preNode = curNode;
            while (curNode != null) {
                if (curNode.value != value) {
                    preNode = curNode;
                    curNode = curNode.next;
                } else {
                    preNode.next = curNode.next;
                    curNode.next = null;
                    break;
                }
            }

        }

    }

    public boolean contains(int value) {
        int hash = hash(value);
        int binNum = size == 0 ? 0 : hash & (arr.length - 1);
        if (arr == null)
            return false;
        if (arr[binNum] != null) {
            Node curNode = arr[binNum];
            while (curNode != null && curNode.value != value) {
                curNode = curNode.next;
            }
            if (curNode != null) {
                return true;
            }
        }
        return false;
    }

    static class Node {
        private int hash;
        private int key;
        private int value;
        private Node next;

        Node(int hash, int key, int value, Node next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("MyHashSet[");

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                Node temp = arr[i];
                while (temp != null) {
                    sb.append(temp.value).append(",");
                    temp = temp.next;
                }
            }
        }
        if (sb.indexOf(",") != -1) {
            sb.deleteCharAt(sb.lastIndexOf(","));
        }
        sb.append("]");
        return sb.toString();
    }

    public int size() {
        return this.size;
    }
    
}
